package com.daoyintech.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Map;

/**
 * Created by xuzhaolin on 15/12/28.
 */
public class SignatureUtil {

    /**
     * 生成 package 字符串
     * @param map
     * @param paternerKey
     * @return
     */
    public static String generatePackage(Map<String, String> map,String paternerKey){
        String sign = generateSign(map,paternerKey);
        Map<String,String> tmap = MapUtil.order(map);
        String s2 = MapUtil.mapJoin(tmap,false,true);
        return s2+"&sign="+sign;
    }



    public static String generateSign(Map<String, String> map,String paternerKey){
        Map<String, String> tmap = MapUtil.order(map);
        if(tmap.containsKey("sign")){
            tmap.remove("sign");
        }
        String str = MapUtil.mapJoin(tmap, false, false);
        return DigestUtils.md5Hex(str+"&key="+paternerKey).toUpperCase();
    }


}
